<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar
      title="编辑资料"
      left-arrow
      @click-left="$router.back()"
      fixed
    />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image
            round
            class="avatar"
            :src="user.photo"
            @click="$refs.iptFile.click()"
          />
          <input
            type="file"
            ref="iptFile"
            v-show="false"
            accept="image/*"
            @change="onFileChange"
          />
        </template>
      </van-cell>
      <van-cell
        title="名称"
        is-link
        :value="user.name"
        @click="changeNameFn()"
      />
      <!-- 修改姓名弹框 -->
      <van-dialog
        v-model="isShowNameDialog"
        title="修改名称"
        show-cancel-button
        :before-close="onNameDialogBeforeClose"
        v-fofo
      >
        <!-- 输入框 -->
        <van-field
          v-model.trim="userName"
          input-align="center"
          maxlength="7"
          placeholder="请输入名称"
          v-fofo
        />
      </van-dialog>
      <van-cell
        title="生日"
        is-link
        :value="user.birthday"
        @click="birthdayFn"
      />
      <van-popup v-model="show" position="bottom" :style="{ height: '30%' }">
        <van-datetime-picker
          v-model="iscurrentDate"
          type="date"
          title="选择年月日"
          :min-date="minDate"
          :max-date="maxDate"
        />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import {
  getoneUserMessageAPI,
  changeUserPhotoMessageAPI,
  changeUserMessageAPI
} from '@/api'
import { Notify, Toast } from 'vant'
export default {
  name: 'UserEdit',
  data () {
    return {
      user: {},
      isShowNameDialog: false,
      userName: '', // 编辑用户名
      show: false,
      minDate: new Date(2020, 0, 1),
      maxDate: new Date(2025, 10, 1),
      currentDate: new Date()
    }
  },
  async created () {
    const res = await getoneUserMessageAPI()
    console.log(res)
    this.user = res.data.data
    console.log(this.user)
    this.userName = this.user.name
  },
  methods: {
    // 文件选择方法
    async onFileChange (ev) {
      //   console.log(ev.target.files[0])
      if (ev.target.files.length === 0) return // 防止用户未选择图片
      const fd = new FormData()
      fd.append('photo', ev.target.files[0]) // photo在表单里参数名携带
      const res = await changeUserPhotoMessageAPI(fd)
      console.log(res)
      this.user.photo = res.data.data.photo // 更新最新头像
    },
    // 姓名
    async changeNameFn () {
      this.isShowNameDialog = true
    },
    // 姓名修改弹出层-关闭前方法
    async onNameDialogBeforeClose (action, done) {
      if (action === 'confirm') {
        console.log(this.userName)
        if (this.userName === this.user.name) {
          Toast('未修改姓名')
          done(false)
        } else {
          if (/^[A-Za-z\u4e00-\u9fa5]{1,7}$/.test(this.userName)) {
            const res = await changeUserMessageAPI({
              name: this.userName,
              birthday: this.user.birthday
            })
            done()
            const res1 = await getoneUserMessageAPI()
            this.user.name = res1.data.data.name
          } else {
            Notify({ type: 'warning', message: '姓名只能在1-7位的中英文字符' })
            done(false)
          }
        }
      } else {
        done()
      }
    },
    // 生日
    birthdayFn () {
      this.show = true
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
</style>
